package com.example.orderservice.config;

import com.example.orderservice.model.Order;
import com.example.orderservice.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.UUID;

@Configuration
public class DataInitializer {

    @Bean
    public CommandLineRunner initData(OrderRepository repository) {
        return args -> {
            // 创建并保存几个示例订单
            Order order1 = new Order();
            order1.setOrderNumber(UUID.randomUUID().toString());
            order1.setProductId(1L);
            order1.setQuantity(2);
            order1.setTotalAmount(new BigDecimal("199.98"));
            order1.setOrderDate(LocalDateTime.now());
            order1.setStatus("已完成");
            
            Order order2 = new Order();
            order2.setOrderNumber(UUID.randomUUID().toString());
            order2.setProductId(2L);
            order2.setQuantity(1);
            order2.setTotalAmount(new BigDecimal("299.99"));
            order2.setOrderDate(LocalDateTime.now());
            order2.setStatus("处理中");
            
            Order order3 = new Order();
            order3.setOrderNumber(UUID.randomUUID().toString());
            order3.setProductId(3L);
            order3.setQuantity(3);
            order3.setTotalAmount(new BigDecimal("89.97"));
            order3.setOrderDate(LocalDateTime.now());
            order3.setStatus("已发货");
            
            repository.save(order1);
            repository.save(order2);
            repository.save(order3);
            
            System.out.println("订单服务 - 初始化数据完成");
        };
    }
} 